﻿using System;
using System.Data;
using System.IO;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NVelocity;
using NVelocity.App;
using NVelocity.Context;

/// <summary>
/// VelocityUtil 的摘要描述
/// </summary>
public class VelocityUtil
{
    protected VelocityEngine velocity = new VelocityEngine();
    protected VelocityContext context = new VelocityContext();
    protected string fileName;

    public VelocityUtil(string base_dir, string fileName) : this(base_dir + @"\" + fileName)
    {
    }

	public VelocityUtil(string fileName)
	{
        this.fileName = GetLocalize(fileName).Replace("~", "");

        Commons.Collections.ExtendedProperties props = new Commons.Collections.ExtendedProperties();

        // fix bug
        //props.AddProperty("file.resource.loader.path", HttpContext.Current.Server.MapPath(@"~\App_Data"));
        //props.AddProperty("file.resource.loader.path", dir);
        props.AddProperty("file.resource.loader.path", HttpRuntime.AppDomainAppPath);
        
        props.AddProperty("input.encoding", "utf-8");
        props.AddProperty("output.encoding", "utf-8");

        velocity.Init(props);

        context.Put("MyUtil", new MyUtil());
        //context.Put("MyDate", MyDate.DBNow);
	}

    public static string GetLocalize(string fileName)
    {
        string dir = VirtualPathUtility.GetDirectory(fileName);
        string name = VirtualPathUtility.GetFileName(fileName);

        string localized = dir + @"\" + Locale + @"\" + name;

        if (File.Exists(HttpContext.Current.Server.MapPath(localized)))
        {
            return localized;
        }
        else
        {
            return fileName;
        }

    }

    public void Put(string key, object value)
    {
        if (value is DataRow)
        {
            foreach(DataColumn dc in (value as DataRow).Table.Columns)
            {
                if (dc.ReadOnly == false && (value as DataRow)[dc] is string)
                {
                    (value as DataRow)[dc] = HttpUtility.HtmlEncode((value as DataRow)[dc].ToString());
                }
            }
        }

        context.Put(key, value);
    }

    public void Put(VelocityContext context)
    {
        foreach (string key in context.Keys)
        {
            this.context.Put(key, context.Get(key));
        }

        //this.context = context;
    }

    public virtual string Output()
    {
        Template t = velocity.GetTemplate(this.fileName);

        using (StringWriter sr = new StringWriter())
        {
            t.Merge(context, sr);

            return sr.GetStringBuilder().ToString();
        }
    }

    public void Download()
    {
        MyObj.DownloadFile(VirtualPathUtility.GetFileName(fileName), Encoding.UTF8.GetBytes(Output()));
    }

    static string locale;

    public static string Locale
    {
        get
        {
            return "zh-tw";
        }
    }

    public class MyUtil
    {
        public CodeManager codemanager
        {
            get
            {
                return CodeManager.Current;
            }
        }

        public string Format(string format, params object[] args)
        {
            return string.Format(format, args);
        }

        public DateTime ToDateTime(string o)
        {
            DateTime d;

            if (DateTime.TryParse(o, out d))
            {
                return d;
            }
            else
            {
                return DateTime.MinValue;
            }
        }
    }
}
